<template>
  <div v-show="isShow" class="open-menu">
    <div class="dis-flex menu-item menu-hover" @click="setBread('file')">
      <div class="matlab-icon icon-open-file" />
      打开...
    </div>
    <div class="sub-title">最近使用的文件</div>
    <div
      v-for="(item, index) in $store.state.matlab.fileList"
      :key="index"
      class="file-bg menu-hover"
      @click="openFile(item)"
    >
      <div class="file-title">
        <svg-icon :icon-class="getIconName(item)" />
        <div class="file-name ml-10">{{ item.label }}</div>
      </div>
      <div class="file-path">{{ `${item.path}/${item.label}` }}</div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'OpenMenu',
  data() {
    return {
      isShow: false
    };
  },
  inject: ['setBread'],
  methods: {
    show() {
      this.isShow = true;
    },
    hide() {
      this.isShow = false;
    },
    openFile(item) {
      this.$store.dispatch('matlab/getFile', item);
    },
    setTreeData() {
      this.$emit('openFile');
    },
    getIconName(item) {
      const name = item.label;
      const isFile = item.isFile;
      const supportedFileTypes = ['json', 'py', 'txt', 'yaml'];
      const suffix = name.slice(name.lastIndexOf('.') + 1);
      if (isFile) {
        return supportedFileTypes.includes(suffix) ? suffix : 'file';
      } else {
        if (item.projectId || item.tmpProjectIcon) {
          return 'project';
        }
        return 'folderopen';
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.open-menu {
  background: #fff;
  border: 1px solid #7a7a7a;
  text-align: left;
  width: max-content;
  min-width: 120px;
  max-height: calc(100vh - 120px);
  overflow-y: auto;
  z-index: 1;

  .menu-item {
    height: 36px;
    line-height: 36px;
    border: 1px solid transparent;
  }

  .menu-hover:hover {
    border: 1px solid #0099ff;
    background: #f5f5f5;
  }

  .sub-title {
    background: #e0e0e0;
    height: 27px;
    line-height: 27px;
    color: #242424;
    font-weight: bold;
    padding-left: 5px;
  }

  .file-bg {
    padding: 10px;
    border: 1px solid transparent;

    .file-title {
      display: flex;
      align-items: center;

      .file-name {
        color: #005695;
        font-weight: bold;
        overflow-wrap: break-word;
        white-space: pre-line;
      }
    }

    .file-path {
      font-size: 11px;
      overflow-wrap: break-word;
      white-space: pre-line;
    }
  }

  .icon-open-file {
    width: 24px;
    height: 24px;
    margin: 6px;
  }
}
</style>
